<template>
  <div class="gender-edit-container">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="$emit('close')"
      :default-index="genderDefalut"
    />
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'

export default {
  name: 'GenderEdit',
  props: {
    userGender: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      genderDefalut: this.userGender === 0 ? 0 : 1
    }
  },
  methods: {
    async onConfirm (value, index) {
      try {
        const { data: res } = await updateUserProfileAPI({
          gender: index
        })
        console.log(res)
        // 更新视图
        this.$emit('update:userGender', index)
        // this.$toast(`当前值：${value}, 当前索引：${index}`)
        // 关闭弹框
        this.$emit('close')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped>

</style>
